//
//  AlbumTableViewCell.m
//  Findif
//
//  Created by appdev on 2018/10/29.
//  Copyright © 2018 Cicel Chen. All rights reserved.
//

#import "AlbumTableViewCell.h"


@interface AlbumTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, weak)UILabel * countLabel;

@end

@implementation AlbumTableViewCell


- (void)setArray:(NSArray *)array{
    
    _array = array;

    self.countLabel.text = [NSString stringWithFormat:@"%ld张",array.count];
    
    if (array.count > 0) {

        UIImageView * oneImageView = [UIImageView new];
        [self.contentView addSubview:oneImageView];
        UIImageView * twoImageView = [UIImageView new];
        [self.contentView addSubview:twoImageView];
        UIImageView * threeImageView = [UIImageView new];
        [self.contentView addSubview:threeImageView];
        
        if (array.count == 1) {
            
            [twoImageView removeFromSuperview];
            [threeImageView removeFromSuperview];
            
            NSDictionary * dict = array.firstObject;
            
            //        UIImageView * imageView = [UIImageView new];
            
            NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"img"]];
            NSURL * url = [NSURL URLWithString:imgStr];
            [oneImageView sd_setImageWithURL:url];
            
            [oneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.contentView);
                make.height.width.mas_equalTo(40 * SCREENH6);
                make.leading.mas_equalTo(self.contentView).offset(80 * SCREENH6);
            }];
        }else if (array.count == 2){
            
            
            [threeImageView removeFromSuperview];
            
            NSDictionary * oneDict = array.firstObject;
            NSDictionary * twoDict = array[1];
            
            //        UIImageView * oneImageView = [UIImageView new];
            
            NSString * oneImgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,oneDict[@"img"]];
            NSURL * oneUrl = [NSURL URLWithString:oneImgStr];
            [oneImageView sd_setImageWithURL:oneUrl];
            
            //        [self.contentView addSubview:oneImageView];
            
            [oneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.contentView);
                make.height.width.mas_equalTo(40 * SCREENH6);
                make.leading.mas_equalTo(self.contentView).offset(80 * SCREENH6);
            }];
            
            //        UIImageView * twoImageView = [UIImageView new];
            
            NSString * twoImgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,twoDict[@"img"]];
            NSURL * twoUrl = [NSURL URLWithString:twoImgStr];
            [twoImageView sd_setImageWithURL:twoUrl];
            
            //        [self.contentView addSubview:twoImageView];
            
            [twoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.height.width.mas_equalTo(oneImageView);
                make.leading.mas_equalTo(oneImageView.mas_trailing).offset(5 * SCREENH6);
            }];
            
        }else if(array.count > 2){
            
            NSDictionary * oneDict = array.firstObject;
            NSDictionary * twoDict = array[1];
            NSDictionary * threeDict = array[2];
            
            //        UIImageView * oneImageView = [UIImageView new];
            
            NSString * oneImgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,oneDict[@"img"]];
            NSURL * oneUrl = [NSURL URLWithString:oneImgStr];
            [oneImageView sd_setImageWithURL:oneUrl];
            
            //        [self.contentView addSubview:oneImageView];
            
            [oneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.contentView);
                make.height.width.mas_equalTo(40 * SCREENH6);
                make.leading.mas_equalTo(self.contentView).offset(80 * SCREENH6);
            }];
            
            //        UIImageView * twoImageView = [UIImageView new];
            
            NSString * twoImgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,twoDict[@"img"]];
            NSURL * twoUrl = [NSURL URLWithString:twoImgStr];
            [twoImageView sd_setImageWithURL:twoUrl];
            
            //        [self.contentView addSubview:twoImageView];
            
            [twoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.height.width.mas_equalTo(oneImageView);
                make.leading.mas_equalTo(oneImageView.mas_trailing).offset(5 * SCREENH6);
            }];
            
            //        UIImageView * threeImageView = [UIImageView new];
            
            NSString * threeImgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,threeDict[@"img"]];
            NSURL * threeUrl = [NSURL URLWithString:threeImgStr];
            [threeImageView sd_setImageWithURL:threeUrl];
            
            //        [self.contentView addSubview:threeImageView];
            
            [threeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.height.width.mas_equalTo(twoImageView);
                make.leading.mas_equalTo(twoImageView.mas_trailing).offset(5 * SCREENH6);
            }];
        }
    }else{
        
        for(UIImageView * imageView in [self.contentView subviews])
        {
            if ([imageView isKindOfClass:[UIImageView class]]) {
                
                if (self.rightImageView == imageView) {
                    
                }else{
                    
                    [imageView removeFromSuperview];
                }
                
            }
        }
    }
    
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"相册";
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.contentView addSubview:self.titleLabel];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"右箭头"];
    
    self.rightImageView = rightImageView;
    
    [self.contentView addSubview:self.rightImageView];
    
    
    UILabel * countLabel = [UILabel new];
    
    countLabel.text = @"----";
    countLabel.textColor = [UIColor lightGrayColor];
    countLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.countLabel = countLabel;
    
    [self.contentView addSubview:self.countLabel];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(60 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView.mas_leading).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.rightImageView);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
